package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SealImg;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.service.JmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SealImgMapper;
import com.ruoyi.system.service.ISealImgService;

import java.util.List;

/**
 * 印章图片Service业务层处理
 *
 * @author ess
 * @date 2021-10-25
 */
@Service
public class SealImgServiceImpl implements ISealImgService
{
    @Autowired
    private SealImgMapper sealImgMapper;
    @Autowired
    private JmService jmService;

    /**
     * 查询印章图片
     *
     * @param imgId 印章图片主键
     * @return 印章图片
     */
    @Override
    public SealImg selectSealImgByImgId(String imgId)
    {
        return getDSealImg(sealImgMapper.selectSealImgByImgId(imgId));
    }

    /**
     * 查询印章图片列表
     *
     * @param sealImg 印章图片
     * @return 印章图片
     */
    @Override
    public List<SealImg> selectSealImgList(SealImg sealImg)
    {
        List<SealImg> sealImgList = sealImgMapper.selectSealImgList(getESealImg(sealImg));
        if (sealImgList != null && sealImgList.size() > 0) {
            for (SealImg a:sealImgList) {
                getDSealImg(a);
            }
            return sealImgList;
        }
        return sealImgList;
    }

    /**
     * 新增印章图片
     *
     * @param sealImg 印章图片
     * @return 结果
     */
    @Override
    public int insertSealImg(SealImg sealImg)
    {
        return sealImgMapper.insertSealImg(getESealImg(sealImg));
    }

    /**
     * 修改印章图片
     *
     * @param sealImg 印章图片
     * @return 结果
     */
    @Override
    public int updateSealImg(SealImg sealImg)
    {
        return sealImgMapper.updateSealImg(getESealImg(sealImg));
    }

    /**
     * 批量删除印章图片
     *
     * @param imgIds 需要删除的印章图片主键
     * @return 结果
     */
    @Override
    public int deleteSealImgByImgIds(String[] imgIds)
    {
        return sealImgMapper.deleteSealImgByImgIds(imgIds);
    }

    /**
     * 删除印章图片信息
     *
     * @param imgId 印章图片主键
     * @return 结果
     */
    @Override
    public int deleteSealImgByImgId(String imgId)
    {
        return sealImgMapper.deleteSealImgByImgId(imgId);
    }

    //加密
    private SealImg getESealImg(SealImg sealImg) {
        if (sealImg==null){
            return null;
        }
        if (sealImg.getImgData() != null) {
            sealImg.setImgData(jmService.getEncryptString(sealImg.getImgData()));
        }
        return sealImg;
    }
    //解密
    private SealImg getDSealImg(SealImg sealImg) {
        if (sealImg==null){
            return null;
        }
        if (sealImg.getImgData() != null) {
            sealImg.setImgData(jmService.getDecryptString(sealImg.getImgData()));
        }
        return sealImg;
    }
}
